package com.example.service.serviceImpl;

import com.example.mapper.LoginMapper;
import com.example.pojo.LoginInfo;
import com.example.pojo.User;
import com.example.service.LoginService;
import com.example.utils.Authentication;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Slf4j
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;

    /**
     * 登录
     */
    @Override
    public LoginInfo login(User user) throws IOException {
        User result = loginMapper.findByUsernameAndPassword(user.getUsername(), user.getPassword());
        if (result != null) {
            log.info("登录成功: {}", result);
            return new LoginInfo(result.getId(), result.getUsername(), Authentication.getToken());
        }

        //用户不存在
        return null;
    }
}
